<template>
  <router-layout>
    <div class="container van-hairline--top">
      <!-- 暂无数据组件 -->
      <Nodata v-if="isNoData"></Nodata>
      <div v-else class="content-box">
        <van-list v-model="loading" :finished="finished" @load="onLoad" :offset="0"  v-for="(item, index) in dataList" :key="index">
              <van-swipe-cell :right-width="65" :on-close="onClose" @click="acivt(item)">
                   <van-cell-group>
                        <van-cell :value="$route.query.id==1?'编辑':'查看'" @click="edit(item)">
                            <template slot="title">
                            <p class="van-cell-text one-txt-cut">{{item.proposal_title}}</p>
                            <p class="van-cell-time">{{item.create_time}}</p>
                            </template>
                        </van-cell>
                    </van-cell-group>
                    <span slot="right">删除</span>
              </van-swipe-cell>
        </van-list>
      </div>
    </div>
  </router-layout>
</template>

<script>
import Nodata from "../public/nodata/Nodata"; // 导入暂无数据组件
import { proposal_listMy, proposal_listMyCollect, proposal_delete } from "@/api/proposal";
import { clacTime } from "@/utils";
import TitleItem from "../../components/public/title/title";
export default {
  name: "proposalMyPost",
  components: { Nodata, TitleItem },
  data() {
    return {
      isNoData: false,
      loading: false,
      finished: false,
      listQuery: {
        start: 0,
        current: 1,
        limit: 5,
        company_id: localStorage.getItem("companyId")
      },
      dataList: [],
      total: ""
    };
  },
  methods: {
    acivt(item){
      this.acivtId = item.id
    },
     onClose(clickPosition, instance) {
      switch (clickPosition) {
        case 'left':
        case 'cell':
        case 'outside':
          instance.close();
          break;
        case 'right':
         this.$dialog.confirm({
            message: '确定删除吗？'
          }).then(() => {
            this.dele()
            instance.close();
          });
          break;
      }
    },
    async dele(){
        let par = {
            id:this.acivtId
        }
         let {data}= await proposal_delete(par)
         if(data.status ==100){
             this.$toast('删除成功')
             this.initList()

         }
    },
    //编辑贴子
    edit(item) {
      if (this.$route.query.id == 1) {
        this.$router.push({
          path: "/proposal",
          query: {
            name: "edit",
            content: item
          }
        });
      } else {
        this.$router.push({
          path: "/proposalDetail",
          query: {
            id: item.id
          }
        });
      }
    },
    //获取数据
    async initList() {
      let user = JSON.parse(localStorage.getItem("user"));
      this.listQuery.create_user = user.id;
      let query = Object.assign(this.listQuery);
      let res =
        this.$route.query.id == 1
          ? await proposal_listMy(query)
          : await proposal_listMyCollect(query);
      let result = res.data;
      if (result.status == 100) {
        this.dataList = result.data.rows.map(item => {
          return Object.assign({}, item, {
            create_time: clacTime(item.create_time)
          });
        });
        this.total = result.data.results;
        if (!this.total) {
          this.isNoData = true;
        }
      }
      this.loading = false;
      if (result.data.results <= this.dataList.length) {
        this.finished = true;
      }
    },
    onLoad() {
      setTimeout(() => {
        this.listQuery.limit += 2;
        this.initList();
      }, 1000);
    }
  },
  created() {
    this.initList();
  }
};
</script>

<style lang="scss" scoped>
@import "proposalMyPost";
</style>
